package com.wizz.trade.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.mapper.CurrencyMapper;
import com.wizz.trade.trading.dto.SpotExrateDto;
import com.wizz.trade.trading.dto.page.ImportCostPageDto;
import com.wizz.trade.trading.entity.ImportCost;
import com.wizz.trade.trading.entity.Position;
import com.wizz.trade.trading.mapper.ImportCostMapper;
import com.wizz.trade.trading.mapper.PositionMapper;
import com.wizz.trade.trading.service.ImportCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wizz.trade.trading.vo.ImportCostVo;
import com.wizz.trade.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 进口成本参考表 服务实现类
 * </p>
 *
 * @author fyftless
 * @since 2023-04-23 04:37:04
 */
@Service
public class ImportCostServiceImpl extends ServiceImpl<ImportCostMapper, ImportCost> implements ImportCostService {

    @Resource
    private ImportCostMapper importCostMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private CurrencyMapper currencyMapper;

    @Override
    public void addImportCost(ImportCost importCost) {
        importCostMapper.insert(importCost);
    }

    @Override
    public void updateImportCost(ImportCost importCost) {
        importCostMapper.updateById(importCost);
    }

    @Override
    public void deleteImportCost(Integer id) {
        importCostMapper.deleteById(id);
    }

    @Override
    public PageVo<ImportCostVo> getPage(ImportCostPageDto importCostPageDto) {
        LambdaQueryWrapper<ImportCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(importCostPageDto.getPosition()),
                ImportCost::getPosition, importCostPageDto.getPosition());
        IPage<ImportCost> rawPage = importCostMapper.selectPage(PageUtil.initPage(importCostPageDto), queryWrapper);
        List<ImportCost> records = rawPage.getRecords();
        List<ImportCostVo> importCostVos = records.stream().map((item)->{
            ImportCostVo importCostVo = new ImportCostVo();
            BeanUtils.copyProperties(item, importCostVo);
            return importCostVo;
        }).collect(Collectors.toList());
        return new PageVo<>(rawPage.getPages(),rawPage.getTotal(),importCostVos);
    }

    @Override
    public void setExRate(List<SpotExrateDto> list) {
        for (SpotExrateDto spotExrateDto : list) {
            ImportCost importCost = importCostMapper.selectById(spotExrateDto.getId());
            Position position = positionMapper.selectById(importCost.getPosition());
            importCost.setExrate(spotExrateDto.getExrate());
            importCost.setCostprice(position.getCprice().multiply(BigDecimal.valueOf(spotExrateDto.getExrate())));
            if(Objects.nonNull(importCost.getSettleprice())){
                importCost.setReffpl((importCost.getSettleprice().subtract(importCost.getCostprice())).
                        multiply(position.getOi()));
            }
            importCostMapper.updateById(importCost);
        }
    }

    @Override
    public Map<String, String> getExRate() {
        List<Integer> exrateIds = importCostMapper.getExrate();
        List<Integer> allIds = importCostMapper.getAllIds();
        HashMap<String, String> map = new HashMap<>(16);
        int j = allIds.size()-1;
        for (int i = exrateIds.size() - 1; i >= 0; i--){
            Integer realId = exrateIds.get(i);
            StringBuilder ids = new StringBuilder();
            for (; j>=0; j--){
                ids.append(allIds.get(j));
                if (allIds.get(j).equals(realId)){
                    j--;
                    break;
                }
                ids.append(",");
            }
            Position position = positionMapper.selectById(importCostMapper.selectById(realId).getPosition());
            map.put(currencyMapper.selectById(position.getCurrency()).getName(), ids.toString());
        }
        return map;
    }

}
